<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>基础设置</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/recommend' }"
        >首页推荐信息</el-breadcrumb-item
      >
      <el-breadcrumb-item>添加首页推荐</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <div class="form_title">
        <h5>首页推荐添加</h5>
      </div>
      <!-- 添加首页推荐表单 -->
      <el-form ref="form" :model="form" label-width="95px" :rules="formRules">
        <el-form-item label="图片名称：" prop="name">
          <el-col :span="11">
            <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="上传图片：">
          <div class="uploadButton">
            <el-button size="small" type="primary" @click="showDialog"
              >点击上传</el-button
            ><span> 只能上传jpg/png文件 </span>
          </div>
          <el-image
            class="uploadImage"
            style="width: 60px; height: 100px"
            :src="url"
          ></el-image>
        </el-form-item>
        <el-form-item label="分类：">
          <el-col :span="12">
            <el-select
              v-model="form.type"
              filterable
              placeholder="请选择"
              value-key="id"
              style="width: 100%"
              @change="select"
            >
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="指定：">
          <el-col :span="12">
            <el-select
              v-model="form.correspondingId"
              placeholder="请选择"
              value-key="id"
              style="width: 100%"
            >
              <el-option
                v-for="item in dataList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item class="formbutton">
          <el-button
            class="formsubmit"
            type="primary"
            @click="onSubmit"
            :disabled="disabled"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 剪裁组件弹窗 -->
      <el-dialog
        title="裁切图片"
        :visible.sync="cropperModel"
        width="1200px"
        center
      >
        <cropper-image
          :Name="cropperName"
          ref="child"
          @uploadImgSuccess="handleUploadSuccess"
          :autoCropWidth="autoCropWidth"
          :autoCropHeight="autoCropHeight"
          :fixedNumber="fixedNumber"
        >
        </cropper-image>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import CropperImage from "@/components/CropperImage";
export default {
  components: { CropperImage },
  data() {
    return {
      //裁切图片参数
      cropperModel: false,
      cropperName: "",
      imgName: "",
      imgVisible: false,
      autoCropWidth: 170, //默认生成截图框宽度
      autoCropHeight: 210, //默认生成截图框高度
      fixedNumber:[0.54,1],
      url: "",
      labelPosition: "right",
      dataList: [],
      typeList: [
        { id: 1, name: "奶茶" },
        { id: 2, name: "商品" },
        // { id: 3, name: "活动" },
      ],
      form: {
        name: "",
        image: "",
        type: "",
        correspondingId: "",
      },
      //分页
      queryInfo: {
        pageNo: 1,
        pageSize: 100000,
      },
      formRules: {
        // 验证用户名是否合法
        name: [
          { required: true, message: "请输入首页推荐名称", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //显示修改截图框
    showDialog() {
      this.cropperModel = true;
    },
    //上传图片
    handleUploadSuccess(data) {
      console.log(data);
      this.form.image = data;
      this.url = this.$global.imgURL + data;
      this.cropperModel = false;
    },
    async select() {
      this.dataList = [];
      if (this.form.type == 1) {
        //调用奶茶接口
        //   const { data: res } = await this.$http.get("milkyTea/listMilkyTea", {
        //   params: this.queryInfo,
        // });
        // console.log("查询所有奶茶",res);
        // this.dataList = res.data.milkyTeaVOs;
        const { data: res } = await this.$http.get("milkyTea/listMilkyTeaAll");
        var milkyTea = res.data.milkyTeaTypeXcxVOs;
        for (var i = 0; i < milkyTea.length; i++) {
          this.dataList = this.dataList.concat(milkyTea[i].milkyTeaVOs);
        }
        return;
      }
      if (this.form.type == 2) {
        //调用商品接口
        //   const { data: res } = await this.$http.get("commodity/listCommodity", {
        //   params: this.queryInfo,
        // });
        // // console.log("查询所有商品",res);
        // this.dataList = res.data.commodityVoList;
        const { data: res } = await this.$http.get(
          "commodity/listCommodityInXCX"
        );
        var commodity = res.data.type;
        for (var i = 0; i < commodity.length; i++) {
          this.dataList = this.dataList.concat(commodity[i].commoditys);
        }
      }
    },
    //提交调用相应的接口
    onSubmit() {
      console.log("表单信息", this.form);
      this.$refs.form.validate(async (valid) => {
        if (this.form.type === "") {
          this.$message.error("请选择指定分类");
          return;
        }
        if (this.form.correspondingId === "") {
          this.$message.error("请选择指定商品");
          return;
        }
        if (this.form.image === "") {
          this.$message.error("请上传图片");
          return;
        }
        console.log("valid:" + valid);
        if (valid) {
          console.log(this.form);
          console.log("submit!");
          //调用提交接口
          const { data: res } = await this.$http.post(
            "recommend/insertRecommend",
            this.form
          );
          console.log(res);
          if (res.code != 200) {
            this.$message.error(res.massage);
            return;
          }
          this.$message.success("添加首页推荐成功！");
          this.$router.replace("/recommend");
        } else {
          console.log("表单问题");
          return;
        }
      });
    },
    handleRemove(file, fileList) {
      this.form.image = "";
    },
    handlePreview(response) {
      console.log(response);
      this.form.image = response;
    },
    handleExceed(files, fileList) {
      this.$message.warning("超出1个文件，请先删除当前文件，再重新上传");
      return false;
    },
  },
  created() {},
};
</script>

<style lang="less" scoped>
.form_title {
  height: 40px;
  text-align: left;
  line-height: 0px;
  letter-spacing: 1px;
  border-left: rgba(64, 158, 255, 0.781) solid 3px;
  box-shadow: 0 3px 8px #ddd;
  padding-left: 10px;
  margin-top: 0px;
  overflow: hidden;
}

.el-form {
  width: 800px;
  margin: 50px auto;
  text-align: center;
  padding-left: 350px;
}
.formbutton,
.state {
  position: relative;
}
.formbutton .formsubmit,
.state .stateRadio {
  position: absolute;
  left: 80px;
}
</style>
